﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

namespace UI
{

    public class UIBackpackItem : MonoBehaviour, IPointerDownHandler
    {
        private Text textName,textCount;
        private string strItemTag;

        public UIBackpackItem InitUIData(string itemTag,int count)
        {
            if (textName == null) textName = transform.Find("NameText").GetComponent<Text>();
            if (textCount == null) textCount = transform.Find("CountText").GetComponent<Text>();

            strItemTag = itemTag;
            textName.text = Item.ItemDataMgr.Ins[itemTag]?.strItemName;
            textCount.text = count.ToString();

            return this;
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            //UIBackpack.Ins.IsOpen = false;
            CreateItemAsync();
        }

        private async void CreateItemAsync()
        {
            Item.ItemBase item= (await Item.ItemFactory.CreateItem(strItemTag));

            if (Input.GetMouseButton(0))
            {
                //鼠标还没松开
                item.BeginFollowMouse();
            }
            else
            {
                //异步创建完，鼠标已经松开了，直接删除，避免异常
                Destroy(item.gameObject);
            }
        }
    }

}